package com.yjs.ppjokedemo.ui.sofa;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.yjs.ppjokedemo.R;
import com.yjs.ppjokedemo.adapter.PicListAdapter;
import com.yjs.ppjokedemo.bean.PicProduct;
import com.yjs.ppjokedemo.bean.VideoEntity;
import com.yjs.ppjokedemo.databinding.FragmentPicBinding;
import com.yjs.ppjokedemo.viewmodel.PicViewModel;

import java.util.List;

@Route(path = "/app/PicFragment")
public class PicFragment extends Fragment implements PicListAdapter.PicInterface {

    FragmentPicBinding mFragmentPicBinding;
    private PicViewModel mPicViewModel;
    private PicListAdapter mPicListAdapter;
    NavController mNavController;

    public PicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mFragmentPicBinding = FragmentPicBinding.inflate(inflater,container,false);
        return mFragmentPicBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

//        mNavController = Navigation.findNavController(view);

        mPicListAdapter = new PicListAdapter(this);
        mFragmentPicBinding.rvHomePic.setAdapter(mPicListAdapter);

        mPicViewModel = new ViewModelProvider(requireActivity()).get(PicViewModel.class);

        mPicViewModel.getPicProducts().observe(getViewLifecycleOwner(), new Observer<List<PicProduct>>() {
            @Override
            public void onChanged(List<PicProduct> picProducts) {
                mPicListAdapter.submitList(picProducts);
            }
        });

//        mFragmentPicBinding.rvHomePic.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(getContext(), "跳转", Toast.LENGTH_SHORT).show();
////                ARouter.getInstance()
////                        .build("/app/SofaPicDetailActivity")
////                        .withSerializable("dataBean", VideoEntity.DataBean.class)
////                        .navigation(getContext());
//            }
//        });

    }

    @Override
    public void onItemClick(PicProduct picProduct){
        Log.i("TagA", "点击了item:" + picProduct.getName());

//        mPicViewModel.setPicProduct(picProduct);
//        mNavController.navigate(R.id.action_sofaFragment_to_sofaPicDetailActivity);

    }



}